﻿/// <reference path="libs/jquery.d.ts" />


	export interface JsdlException {
		code: string;
		message?: string;
		stackTrace?: string;
		data?: any; 
	}

	export interface Person
	{	
		firstName: string;
		
		lastName: string;
		
	}
	
	export var baseUrl = "."; 
	export var beforeSend: any = undefined;
		export var sum = function(_a: number, _b: number, completed?: (data: number, exception: JsdlException, result: JQueryXHR, textStatus: string) => void)
		{
			var url = baseUrl + "/JsdlServices/DataService?operation=Sum&";
			
		
		var data = JSON.stringify(Array.prototype.slice.call(arguments, 0, completed !== undefined ? arguments.length - 1 : arguments.length));
			$.ajax({
				url: url, 
				data: data,
				beforeSend: beforeSend, 
				type: "post", 
				dataType: "html"
			}).done(function(data, textStatus, result) { 
				if (completed !== undefined) {
					var d = null; 
					try { 
						d = <number>jQuery.parseJSON(data);
					} catch(e) { 
						alert("Operation 'sum' failed: \n\n" + result.responseText);
					}
					completed(d, null, result, textStatus);
				}
			}).fail(function (result, textStatus) {
				var exception = null; 
				if (result.status >= 400) {
					if (result.responseText != "")
						try { exception = jQuery.parseJSON(result.responseText); } catch (e) { exception = { code: "error_" + result.status }; }
					else
						exception = { code: "error_" + result.status };
					console.log("JSDL server exception (operation: sum): " + result.responseText);
				}
				else 
					alert("Operation 'sum' failed: \n\n" + result.responseText);
				
				if (completed !== undefined)
					completed(null, exception, result, textStatus);
			});
		}
		
		export var getPerson = function(_id: number, completed?: (data: Person, exception: JsdlException, result: JQueryXHR, textStatus: string) => void)
		{
			var url = baseUrl + "/JsdlServices/DataService?operation=GetPerson&";
			
		
		var data = JSON.stringify(Array.prototype.slice.call(arguments, 0, completed !== undefined ? arguments.length - 1 : arguments.length));
			$.ajax({
				url: url, 
				data: data,
				beforeSend: beforeSend, 
				type: "post", 
				dataType: "html"
			}).done(function(data, textStatus, result) { 
				if (completed !== undefined) {
					var d = null; 
					try { 
						d = <Person>jQuery.parseJSON(data);
					} catch(e) { 
						alert("Operation 'getPerson' failed: \n\n" + result.responseText);
					}
					completed(d, null, result, textStatus);
				}
			}).fail(function (result, textStatus) {
				var exception = null; 
				if (result.status >= 400) {
					if (result.responseText != "")
						try { exception = jQuery.parseJSON(result.responseText); } catch (e) { exception = { code: "error_" + result.status }; }
					else
						exception = { code: "error_" + result.status };
					console.log("JSDL server exception (operation: getPerson): " + result.responseText);
				}
				else 
					alert("Operation 'getPerson' failed: \n\n" + result.responseText);
				
				if (completed !== undefined)
					completed(null, exception, result, textStatus);
			});
		}
		
		export var updatePerson = function(_person: Person, completed?: (data: string, exception: JsdlException, result: JQueryXHR, textStatus: string) => void)
		{
			var url = baseUrl + "/JsdlServices/DataService?operation=UpdatePerson&";
			
		
		var data = JSON.stringify(Array.prototype.slice.call(arguments, 0, completed !== undefined ? arguments.length - 1 : arguments.length));
			$.ajax({
				url: url, 
				data: data,
				beforeSend: beforeSend, 
				type: "post", 
				dataType: "html"
			}).done(function(data, textStatus, result) { 
				if (completed !== undefined) {
					var d = null; 
					try { 
						d = data;
					} catch(e) { 
						alert("Operation 'updatePerson' failed: \n\n" + result.responseText);
					}
					completed(d, null, result, textStatus);
				}
			}).fail(function (result, textStatus) {
				var exception = null; 
				if (result.status >= 400) {
					if (result.responseText != "")
						try { exception = jQuery.parseJSON(result.responseText); } catch (e) { exception = { code: "error_" + result.status }; }
					else
						exception = { code: "error_" + result.status };
					console.log("JSDL server exception (operation: updatePerson): " + result.responseText);
				}
				else 
					alert("Operation 'updatePerson' failed: \n\n" + result.responseText);
				
				if (completed !== undefined)
					completed(null, exception, result, textStatus);
			});
		}
		
		
	
